Imports System.Runtime.Serialization
Imports System.ServiceModel

<DataContract()> _
Public Class Name

    Private _NameId As Guid = Guid.Empty
    Private _NameLSID As String
    Private _NameFull As String
    Private _RankId As Guid = Guid.Empty
    Private _NameRank As Rank
    Private _ParentNameId As Guid = Guid.Empty
    Private _ParentName As Name
    Private _PreferredNameId As Guid = Guid.Empty
    Private _PreferredName As Name
    Private _Canonical As String
    Private _Authors As String
    Private _BasionymAuthors As String
    Private _CombinationAuthors As String
    Private _PublishedIn As String
    Private _ReferenceId As Guid
    Private _Reference As Reference
    Private _YearOfPublication As String
    Private _YearOnPublication As String
    Private _MicroReference As String
    Private _TypeVoucher As String
    Private _TypeName As Name
    Private _TypeNameId As Guid = Guid.Empty
    Private _Orthography As String
    Private _Basionym As Name
    Private _BasionymId As Guid = Guid.Empty
    Private _BasedOn As Name
    Private _BasedOnId As Guid = Guid.Empty
    Private _ConservedAgainst As Name
    Private _ConservedAgainstId As Guid = Guid.Empty
    Private _HomonymOf As Name
    Private _HomonymOfId As Guid = Guid.Empty
    Private _ReplacementFor As Name
    Private _ReplacementForId As Guid = Guid.Empty
    Private _BlockingName As Name
    Private _BlockingNameId As Guid = Guid.Empty
    Private _InCitation As Boolean = False
    Private _Invalid As Boolean = False
    Private _Illegitimate As Boolean = False
    Private _Misapplied As Boolean = False
    Private _ProParte As Boolean = False
    Private _NameFullFormatted As String
    Private _ChangeDate As DateTime = DateTime.MinValue
    Private _ChangeUser As String = ""

    Private _namesInterface As INames

    Public Sub New(ByVal namesInt As INames)
        _namesInterface = namesInt
    End Sub

    Public Function Copy() As Name
        Dim newname As New Name

        newname.Authors = _Authors
        newname.BasedOnId = _BasedOnId
        newname.BasionymAuthors = _BasionymAuthors
        newname.BasionymId = _BasionymId
        newname.BlockingNameId = _BlockingNameId
        newname.Canonical = _Canonical
        newname.ChangeDate = _ChangeDate
        newname.ChangeUser = _ChangeUser
        newname.CombinationAuthors = _CombinationAuthors
        newname.ConservedAgainstId = _ConservedAgainstId
        newname.HomonymOfId = _HomonymOfId
        newname.Illegitimate = _Illegitimate
        newname.InCitation = _InCitation
        newname.Invalid = _Invalid
        newname.MicroReference = _MicroReference
        newname.Misapplied = _Misapplied
        newname.NameFull = _NameFull
        newname.NameFullFormatted = _NameFullFormatted
        newname.NameId = _NameId
        newname.NameLSID = _NameLSID
        newname.Orthography = _Orthography
        newname.ParentNameId = _ParentNameId
        newname.PreferredNameId = _PreferredNameId
        newname.ProParte = _ProParte
        newname.PublishedIn = _PublishedIn
        newname.RankId = _RankId
        newname.ReferenceId = _ReferenceId
        newname.ReplacementForId = _ReplacementForId
        newname.TypeNameId = _TypeNameId
        newname.TypeVoucher = _TypeVoucher
        newname.YearOfPublication = _YearOfPublication
        newname.YearOnPublication = _YearOnPublication

        Return newname
    End Function

    <DataMember()> _
    Public Property NameId() As Guid
        Get
            Return _NameId
        End Get
        Set(ByVal value As Guid)
            _NameId = value
        End Set
    End Property

    <DataMember()> _
    Public Property NameLSID() As String
        Get
            Return _NameLSID
        End Get
        Set(ByVal value As String)
            _NameLSID = value
        End Set
    End Property

    <DataMember()> _
    Public Property NameFull() As String
        Get
            Return _NameFull
        End Get
        Set(ByVal value As String)
            _NameFull = value
        End Set
    End Property

    <DataMember()> _
    Public Property RankId() As Guid
        Get
            Return _RankId
        End Get
        Set(ByVal value As Guid)
            _RankId = value
        End Set
    End Property

    <DataMember()> _
    Public Property NameRank() As Rank
        Get
            Return _NameRank
        End Get
        Set(ByVal value As Rank)
            _NameRank = value
        End Set
    End Property

    <DataMember()> _
    Public Property ParentNameId() As Guid
        Get
            Return _ParentNameId
        End Get
        Set(ByVal value As Guid)
            _ParentNameId = value
        End Set
    End Property

    <DataMember()> _
    Public Property ParentName() As Name
        Get
            If _ParentName Is Nothing AndAlso _ParentNameId <> Guid.Empty AndAlso _namesInterface IsNot Nothing Then
                _ParentName = _namesInterface.GetName(_ParentNameId)
            End If
            Return _ParentName
        End Get
        Set(ByVal value As Name)
            _ParentName = value
        End Set
    End Property

    <DataMember()> _
    Public Property PreferredNameId() As Guid
        Get
            Return _PreferredNameId
        End Get
        Set(ByVal value As Guid)
            _PreferredNameId = value
        End Set
    End Property

    <DataMember()> _
    Public Property PreferredName() As Name
        Get
            If _PreferredName Is Nothing AndAlso _PreferredNameId <> Guid.Empty AndAlso _namesInterface IsNot Nothing Then
                _PreferredName = _namesInterface.GetName(_PreferredNameId)
            End If
            Return _PreferredName
        End Get
        Set(ByVal value As Name)
            _PreferredName = value
        End Set
    End Property

    <DataMember()> _
    Public Property Canonical() As String
        Get
            Return _Canonical
        End Get
        Set(ByVal value As String)
            _Canonical = value
        End Set
    End Property

    <DataMember()> _
    Public Property Authors() As String
        Get
            Return _Authors
        End Get
        Set(ByVal value As String)
            _Authors = value
        End Set
    End Property

    <DataMember()> _
    Public Property BasionymAuthors() As String
        Get
            Return _BasionymAuthors
        End Get
        Set(ByVal value As String)
            _BasionymAuthors = value
        End Set
    End Property

    <DataMember()> _
    Public Property CombinationAuthors() As String
        Get
            Return _CombinationAuthors
        End Get
        Set(ByVal value As String)
            _CombinationAuthors = value
        End Set
    End Property

    <DataMember()> _
    Public Property PublishedIn() As String
        Get
            Return _PublishedIn
        End Get
        Set(ByVal value As String)
            _PublishedIn = value
        End Set
    End Property

    <DataMember()> _
    Public Property ReferenceId() As Guid
        Get
            Return _ReferenceId
        End Get
        Set(ByVal value As Guid)
            _ReferenceId = value
        End Set
    End Property

    <DataMember()> _
    Public Property Reference() As Reference
        Get
            If _Reference Is Nothing AndAlso _namesInterface IsNot Nothing AndAlso _ReferenceId <> Guid.Empty Then
                'todo _Reference = _namesInterface.getr
            End If
            Return _Reference
        End Get
        Set(ByVal value As Reference)
            _Reference = value
        End Set
    End Property

    <DataMember()> _
    Public Property YearOfPublication() As String
        Get
            Return _YearOfPublication
        End Get
        Set(ByVal value As String)
            _YearOfPublication = value
        End Set
    End Property

    <DataMember()> _
    Public Property YearOnPublication() As String
        Get
            Return _YearOnPublication
        End Get
        Set(ByVal value As String)
            _YearOnPublication = value
        End Set
    End Property

    <DataMember()> _
    Public Property MicroReference() As String
        Get
            Return _MicroReference
        End Get
        Set(ByVal value As String)
            _MicroReference = value
        End Set
    End Property

    <DataMember()> _
    Public Property TypeVoucher() As String
        Get
            Return _TypeVoucher
        End Get
        Set(ByVal value As String)
            _TypeVoucher = value
        End Set
    End Property

    <DataMember()> _
    Public Property TypeName() As Name
        Get
            If _TypeName Is Nothing AndAlso _TypeNameId <> Guid.Empty AndAlso _namesInterface IsNot Nothing Then
                _TypeName = _namesInterface.GetName(_TypeNameId)
            End If
            Return _TypeName
        End Get
        Set(ByVal value As Name)
            _TypeName = value
        End Set
    End Property

    <DataMember()> _
    Public Property TypeNameId() As Guid
        Get
            Return _TypeNameId
        End Get
        Set(ByVal value As Guid)
            _TypeNameId = value
        End Set
    End Property

    <DataMember()> _
    Public Property Orthography() As String
        Get
            Return _Orthography
        End Get
        Set(ByVal value As String)
            _Orthography = value
        End Set
    End Property

    <DataMember()> _
    Public Property Basionym() As Name
        Get
            If _Basionym Is Nothing AndAlso _BasionymId <> Guid.Empty AndAlso _namesInterface IsNot Nothing Then
                _Basionym = _namesInterface.GetName(_BasionymId)
            End If
            Return _Basionym
        End Get
        Set(ByVal value As Name)
            _Basionym = value
        End Set
    End Property

    <DataMember()> _
    Public Property BasionymId() As Guid
        Get
            Return _BasionymId
        End Get
        Set(ByVal value As Guid)
            _BasionymId = value
        End Set
    End Property

    <DataMember()> _
    Public Property BasedOn() As Name
        Get
            If _BasedOn Is Nothing AndAlso _BasedOnId <> Guid.Empty AndAlso _namesInterface IsNot Nothing Then
                _BasedOn = _namesInterface.GetName(_BasedOnId)
            End If
            Return _BasedOn
        End Get
        Set(ByVal value As Name)
            _BasedOn = value
        End Set
    End Property

    <DataMember()> _
    Public Property BasedOnId() As Guid
        Get
            Return _BasedOnId
        End Get
        Set(ByVal value As Guid)
            _BasedOnId = value
        End Set
    End Property

    <DataMember()> _
    Public Property ConservedAgainst() As Name
        Get
            If _ConservedAgainst Is Nothing AndAlso _ConservedAgainstId <> Guid.Empty AndAlso _namesInterface IsNot Nothing Then
                _ConservedAgainst = _namesInterface.GetName(_ConservedAgainstId)
            End If
            Return _ConservedAgainst
        End Get
        Set(ByVal value As Name)
            _ConservedAgainst = value
        End Set
    End Property

    <DataMember()> _
    Public Property ConservedAgainstId() As Guid
        Get
            Return _ConservedAgainstId
        End Get
        Set(ByVal value As Guid)
            _ConservedAgainstId = value
        End Set
    End Property

    <DataMember()> _
    Public Property HomonymOf() As Name
        Get
            If _HomonymOf Is Nothing AndAlso _HomonymOfId <> Guid.Empty AndAlso _namesInterface IsNot Nothing Then
                _HomonymOf = _namesInterface.GetName(_HomonymOfId)
            End If
            Return _HomonymOf
        End Get
        Set(ByVal value As Name)
            _HomonymOf = value
        End Set
    End Property

    <DataMember()> _
    Public Property HomonymOfId() As Guid
        Get
            Return _HomonymOfId
        End Get
        Set(ByVal value As Guid)
            _HomonymOfId = value
        End Set
    End Property

    <DataMember()> _
    Public Property ReplacementFor() As Name
        Get
            If _ReplacementFor Is Nothing AndAlso _ReplacementForId <> Guid.Empty AndAlso _namesInterface IsNot Nothing Then
                _ReplacementFor = _namesInterface.GetName(_ReplacementForId)
            End If
            Return _ReplacementFor
        End Get
        Set(ByVal value As Name)
            _ReplacementFor = value
        End Set
    End Property

    <DataMember()> _
    Public Property ReplacementForId() As Guid
        Get
            Return _ReplacementForId
        End Get
        Set(ByVal value As Guid)
            _ReplacementForId = value
        End Set
    End Property

    <DataMember()> _
    Public Property BlockingName() As Name
        Get
            If _BlockingName Is Nothing AndAlso _BlockingNameId <> Guid.Empty AndAlso _namesInterface IsNot Nothing Then
                _BlockingName = _namesInterface.GetName(_BlockingNameId)
            End If
            Return _BlockingName
        End Get
        Set(ByVal value As Name)
            _BlockingName = value
        End Set
    End Property

    <DataMember()> _
    Public Property BlockingNameId() As Guid
        Get
            Return _BlockingNameId
        End Get
        Set(ByVal value As Guid)
            _BlockingNameId = value
        End Set
    End Property

    <DataMember()> _
    Public Property InCitation() As Boolean
        Get
            Return _InCitation
        End Get
        Set(ByVal value As Boolean)
            _InCitation = value
        End Set
    End Property

    <DataMember()> _
    Public Property Invalid() As Boolean
        Get
            Return _Invalid
        End Get
        Set(ByVal value As Boolean)
            _Invalid = value
        End Set
    End Property

    <DataMember()> _
    Public Property Illegitimate() As Boolean
        Get
            Return _Illegitimate
        End Get
        Set(ByVal value As Boolean)
            _Illegitimate = value
        End Set
    End Property

    <DataMember()> _
    Public Property Misapplied() As Boolean
        Get
            Return _Misapplied
        End Get
        Set(ByVal value As Boolean)
            _Misapplied = value
        End Set
    End Property

    <DataMember()> _
    Public Property ProParte() As Boolean
        Get
            Return _ProParte
        End Get
        Set(ByVal value As Boolean)
            _ProParte = value
        End Set
    End Property

    <DataMember()> _
    Public Property NameFullFormatted() As String
        Get
            Return _NameFullFormatted
        End Get
        Set(ByVal value As String)
            _NameFullFormatted = value
        End Set
    End Property

    <DataMember()> _
    Public Property ChangeDate() As DateTime
        Get
            Return _ChangeDate
        End Get
        Set(ByVal value As DateTime)
            _ChangeDate = value
        End Set
    End Property

    <DataMember()> _
    Public Property ChangeUser() As String
        Get
            Return _ChangeUser
        End Get
        Set(ByVal value As String)
            _ChangeUser = value
        End Set
    End Property

    Public Sub New()
    End Sub

    <OperationContract()> _
    Public Shared Function CreateLSID(ByVal nameId As Guid) As String
        Dim lsid As String = ""
        If nameId <> Guid.Empty Then
            lsid = "urn:lsid:" + NamesGlobal.LSIDAuthorityText + ":taxon-name:" + nameId.ToString.ToLower()
        End If
        Return lsid
    End Function


End Class
